package com.ds.applet.service.impl;

import java.util.List;

import com.ds.common.exception.ServiceException;
import com.ds.common.utils.StringUtils;
import com.ds.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.ds.applet.mapper.MiniPledgeNumMapper;
import com.ds.applet.domain.MiniPledgeNum;
import com.ds.applet.service.IMiniPledgeNumService;


import javax.annotation.Resource;

/**
 * 质保编码Service业务层处理
 *
 * @author liucan
 * @date 2024-04-03
 */
@Service
public class MiniPledgeNumServiceImpl implements IMiniPledgeNumService {

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Resource
    private MiniPledgeNumMapper miniPledgeNumMapper;

    /**
     * 查询质保编码
     *
     * @param id 质保编码主键
     * @return 质保编码
     */
    @Override
    public MiniPledgeNum selectMiniPledgeNumById(Long id) {
        return miniPledgeNumMapper.selectMiniPledgeNumById(id);
    }

    /**
     * 查询质保编码列表
     *
     * @param miniPledgeNum 质保编码
     * @return 质保编码
     */
    @Override
    public List<MiniPledgeNum> selectMiniPledgeNumList(MiniPledgeNum miniPledgeNum) {
        return miniPledgeNumMapper.selectMiniPledgeNumList(miniPledgeNum);
    }

    /**
     * 新增质保编码
     *
     * @param miniPledgeNum 质保编码
     * @return 结果
     */
    @Override
    public int insertMiniPledgeNum(MiniPledgeNum miniPledgeNum) {
        //根据用户填写的个数循环次数
        int coZahl = miniPledgeNum.getCoZahl();//个数
        String coRule = miniPledgeNum.getCoRule();//生成规则
        int i1 = 0;
        for (int i = 0; i < coZahl; i++) {
            MiniPledgeNum miniPledgeNum1 = new MiniPledgeNum();
            miniPledgeNum1.setDictId(miniPledgeNum.getDictId());//产品类型 100漆面膜 101改色膜 102太阳膜
            miniPledgeNum1.setCoRule(coRule);//生成规则
            miniPledgeNum1.setCoPlace(miniPledgeNum.getCoPlace());//位数
            miniPledgeNum1.setCoNum(String.format(coRule + "%0" + miniPledgeNum.getCoPlace() + "d", i + 1));//质保编码
            miniPledgeNum1.setBrId(miniPledgeNum.getBrId());//合作商(品牌)
            miniPledgeNum1.setPrId(miniPledgeNum.getPrId());//产品型号
            miniPledgeNum1.setCoZahl(miniPledgeNum.getCoZahl());//个数
//            miniPledgeNum1.setCoSome(i+1);//最后一个到几号了
            miniPledgeNum1.setCoOrder(miniPledgeNum.getCoOrder());//排序
            miniPledgeNum1.setLeId(miniPledgeNum.getLeId());//所属加盟商
            miniPledgeNum1.setToApplet(miniPledgeNum.getToApplet());//所属小程序
            i1 = miniPledgeNumMapper.insertMiniPledgeNum(miniPledgeNum1);
        }

//
//
//        MiniPledgeNum miniPledgeNum1 = new MiniPledgeNum();
//        miniPledgeNum1.setDictId(miniPledgeNum.getDictId());//产品类型
//        miniPledgeNum1.setPrId(miniPledgeNum.getPrId());//产品型号
//        miniPledgeNum1.setCoShow(0);//启用
//        int size = miniPledgeNumMapper.selectMiniPledgeNumList(miniPledgeNum1).size();//根据产品类型、品牌、产品型号查重
//        if (i1 < 1){
//            return miniPledgeNumMapper.insertMiniPledgeNum(miniPledgeNum);
//        }
//        return -1;
        return i1;
    }

    /**
     * 修改质保编码
     *
     * @param miniPledgeNum 质保编码
     * @return 结果
     */
    @Override
    public int updateMiniPledgeNum(MiniPledgeNum miniPledgeNum) {
        return miniPledgeNumMapper.updateMiniPledgeNum(miniPledgeNum);
    }

    /**
     * 批量删除质保编码
     *
     * @param ids 需要删除的质保编码主键
     * @return 结果
     */
    @Override
    public int deleteMiniPledgeNumByIds(Long[] ids) {
        return miniPledgeNumMapper.deleteMiniPledgeNumByIds(ids);
    }

    /**
     * 删除质保编码信息
     *
     * @param id 质保编码主键
     * @return 结果
     */
    @Override
    public int deleteMiniPledgeNumById(Long id) {
        return miniPledgeNumMapper.deleteMiniPledgeNumById(id);
    }

    /**
     * 导入质保编码
     *
     * @param miniPledgeNumList 质保编码数据列表
     * @param userName          当前登录人
     * @return 结果
     */
    @Override
    public String importUser(List<MiniPledgeNum> miniPledgeNumList, String userName) {
        if (StringUtils.isNull(miniPledgeNumList) || miniPledgeNumList.size() == 0) {
            throw new ServiceException("导入质保编码数据不能为空！");
        }
        System.out.println("数据不为空！！！");
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MiniPledgeNum miniPledgeNum : miniPledgeNumList) {
            try {
                miniPledgeNum.setToApplet(userName);//所属小程序
                miniPledgeNumMapper.insertMiniPledgeNum(miniPledgeNum);
                successNum++;
//                successMsg.append("<br/>" + successNum + "、质保编码 " + miniPledgeNum.getCoNum() + " 导入成功");

            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、质保编码 " + miniPledgeNum.getCoNum() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
